<?php

namespace App\Http\Controllers\Api\Home;

use App\Http\Requests\Api\SaidanRequest;
use App\Models\Saidan;
//use Illuminate\Http\Request;
//use App\Http\Controllers\Controller;
use App\Transformers\SaidanTransformer;
use App\Transformers\UsersTransformer;
use Dingo\Api\Http\Request;
use App\Http\Controllers\Api\Controller;

class SaidanController extends Controller
{
    //首页展示
    public function index(Request $request,Saidan $saidan){

        $query = $saidan->query()->where('is_pre','=',1);

        if (!empty($good_id = $request->get('good_id'))){
            $query->where('good_id','=',$good_id);
        }
        if (!empty($user_id = $request->get('user_id'))){
            $query->where('user_id','=',$user_id);
        }
        $result = $query->paginate(20);
        return $this->response->paginator($result,new SaidanTransformer());
    }

    //添加晒单
    public function store(SaidanRequest $request,Saidan $saidan){

        $saidan->user_id = $request->user_id;
        $saidan->good_id = $request->good_id;
        $saidan->good_name = $request->good_name;
        $saidan->contents = $request->contents;

        $saidan->save();

        return $this->response->array(['message'=>'评论成功'])->setStatusCode(201);
    }
}
